package com.jackrain.nea.wms.mq;
/**
 * Created by Administrator on 2017/3/28.
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * Description:
 * Created with IntelliJ IDEA.
 * User: cs
 * Date: 2017/3/28
 * Time: 10:05
 */

@Component
@Slf4j
public class SendMqService {

    @Autowired
    private ProducerBean producerBean;

    /**
     * 发送消息
     *
     * @param dataarry
     * @param ctag
     * @throws Exception
     */
    public SendResult sendMqinfo(JSONArray dataarry, String topic, String ctag, ValueHolder valueHolder) throws Exception {


        Message msg = null;
        //  Expr 就是 tag
        msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataarry));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean
        SendResult sendResult =  ApplicationContextHandle.getBean(SendMqService.class).send(msg, valueHolder);

        if (sendResult != null){
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }


        return sendResult;

    }


    /**
     * 发送消息
     *
     * @param dataobject
     * @param topic
     * @param ctag
     * @throws Exception
     */
    public SendResult sendMqinfo(JSONObject dataobject, String topic, String ctag, ValueHolder valueHolder) throws Exception {

        Message msg = null;
        msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataobject));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = ApplicationContextHandle.getBean(SendMqService.class).send(msg, valueHolder);

        if (sendResult != null){
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;
    }

    /**
     * 无切面
     * @param dataobject
     * @param topic
     * @param ctag
     * @return
     * @throws Exception
     */
    public SendResult sendMqinfo(JSONObject dataobject, String topic, String ctag) throws Exception {

        Message msg = null;
        msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataobject));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = producerBean.send(msg);

        if (sendResult != null){
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;
    }

    public SendResult sendMqinfo(String dataStr, String topic, String ctag, ValueHolder valueHolder) throws Exception {

        Message msg = null;
        msg = new Message(topic, ctag, MsgConvertUtil
                .string2Bytes(dataStr,"utf-8"));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = ApplicationContextHandle.getBean(SendMqService.class).send(msg, valueHolder);

        if (sendResult != null){
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;
    }

    /**
     * 无切面
     * @param dataStr
     * @param topic
     * @param ctag
     * @return
     * @throws Exception
     */
    public SendResult sendMqinfo(String dataStr, String topic, String ctag) throws Exception {

        Message msg = null;
        msg = new Message(topic, ctag, MsgConvertUtil
                .string2Bytes(dataStr,"utf-8"));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = producerBean.send(msg);

        if (sendResult != null){
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;
    }


    public SendResult send(Message msg, ValueHolder valueHolder) {

        log.debug("sended");

        return producerBean.send(msg);

    }


}
